const http=require("http")
const fs=require("fs")
const app=http.createServer((request,response)=>{
    const url=request.url
    if(url.indexOf("test.css")!=-1){
        fs.readFile('test.css', function (err, data) {
            if (err) {
                return console.error(err);
            }
            console.log("异步读取: " + data.toString());
            setTimeout(()=>{
                response.end(data.toString())
            },3000)
         });
         
    }
    else if(url.indexOf("favicon.ico")!=-1){
        response.end()
    }
    else if(url.indexOf("test.png")!=-1){
        const readStream=fs.createReadStream("test.png")
        readStream.pipe(response)
    }
    else{
        fs.readFile('index.html', function (err, data) {
            if (err) {
                return console.error(err);
            }
            console.log("异步读取: " + data.toString());
            response.end(data.toString())
         });
    }
})
app.listen(3000)